<?php
namespace App\Services;

abstract class BaseService
{

    /** @var \App\Managers\BaseManager */
    protected $manager;

    protected $data;                                //参数

    protected $error = '';                               //错误信息

    protected $info = '';                                //返回信息

    protected $rule;                                //


    //
    public function __construct()
    {
        $data = request()->all();
        foreach ($data AS $key => &$val) {
            //对前后空格数据过滤，
            if(!empty($val) && is_string($val)){
                $val = trim($val);
            }
        }

        $this->data = $data;
    }

    /** 获取成功信息 */
    public function getInfo($default = '')
    {
        if (empty($this->info)) {
            $this->info = $default;
        }
        return $this->info;
    }

    /** 获取错误 */
    public function getError($default = '')
    {
        if (!empty($this->manager->getError())) {
            $this->error = $this->manager->getError();
        }
        if (empty($this->error)) {
            $this->error = $default;
        }
        return $this->error;
    }

    /** 设置参数 */
    public function setData($key, $value = '')
    {
        if (is_array($key)) {
            $this->data = $key;
        } else {
            $this->data[$key] = $value;
        }
        return $this;
    }

    public function has($name)
    {
        return !is_null($this->getData($name));
    }

    /**
     * 获取数据
     * @param string $name
     * @param null $default
     * @return array|mixed
     */
    public function getData($name = NULL, $default = null)
    {
        return is_null($name) ? $this->data : (isset($this->data[$name]) ? $this->data[$name] : $default);
    }

    /** 获取列表 */
    public function lists($isPage = true)
    {
        $isPage = $this->getData('is_page', $isPage);
        unset($this->data['is_page']);
        return $this->manager->setData($this->getData())->lists($isPage);
    }


    /** 保存 */
    public function save()
    {
        return $this->manager->setData($this->getData())->save();
    }

    public function delete()
    {
        return $this->manager->setData($this->getData())->delete();
    }

    public function lock()
    {
        return $this->manager->setData($this->getData())->lock();
    }

    /** 获取详情 */
    public function show()
    {
        return $this->manager->setData($this->getData())->show();
    }

    /** 获取详情 */
    public function update()
    {
        return $this->manager->setData($this->getData())->update();
    }


    protected function success($msg = '成功', $data = null)
    {
        return [
            'code' => 0,
            'status' => true,
            'msg' => $msg,
            'data' => $data,
        ];
    }

    protected function failure($msg, $data = null, $code = 1, $status = false)
    {
        return [
            'code' => $code,
            'status' => $status,
            'msg' => $msg,
            'data' => $data,
        ];
    }
}
